<?php

/************************************

EasyCB, Community Forum Software
Copyright (C) 2007  Jonathon D. Keogh <jonathon.keogh@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

$Id: page.board.php 133 2007-09-07 00:32:52Z jonathon.keogh $

************************************/

require "inc.func.php";

// Does the board exist?
$id = isset($_GET['id']) ? $_GET['id'] : 0;
if(!board_exists($id))
{
	page_redirect("page.boards.php");
}

// Is the board a category?
if(board_parent($id) == 0)
{
	page_redirect("page.boards.php");
}

// Get the board stuff
$info = db_query("SELECT * FROM `" . config_get("db_prefix") . "boards` WHERE `ID`='" . db_escape($id) . "';");
$info = db_fetch_assoc($info);

page_header(htmlentities($info['Label']));

page_section();
$boards = Array();
$c = $id;
while(1)
{
	$b = board_parent($c);
	$boards[$b] = $c;
	if($b == 0)
	{
		break;
	}
	$c = $b;
}
print "<a href=\"page.boards.php\">Home</a>";
while(1)
{
	if($boards[$c] == 0)
	{
		break;
	}
	print ": <a href=\"page.board.php?id=" . urlencode($boards[$c]) . "\">" . htmlentities(board_label($boards[$c])) . "</a>";
	if($boards[$c] == $topic['Board'])
	{
		break;
	}
	$c = $boards[$c];
}
page_section();

$boards = db_query("SELECT * FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='" . db_escape($id) . "' ORDER BY `Order` ASC;");
if(db_num_rows($boards) > 0)
{
	$unread_posts = Array();
	$u = db_query("SELECT * FROM `" . config_get("db_prefix") . "unread` WHERE `UserID`='" . db_escape(sess_get("UserID")) . "';");
	$depth = count(explode('|', board_trail($id)));
	while($row = db_fetch_assoc($u))
	{
		$topic = $row['TopicID'];
		
		$board = db_query("SELECT * FROM `" . config_get("db_prefix") . "topics` WHERE `ID`='" . db_escape($topic) . "';");
		$board = db_fetch_assoc($board);
		$board = $board['Board'];
		
		$parent = explode('|', board_trail($board));
		$parent = $parent[$depth];
		
		$unread_posts[$parent] = true;
	}
	page_section();
	print "There are boards that go deeper in to this board - as shown below:";
	print "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
	while($subboard = db_fetch_assoc($boards))
	{
		print "<tr>\n";
		print "<td rowspan=\"2\" width=\"50\" valign=\"middle\" align=\"center\"><img src=\"img/" . (isset($unread_posts[$subboard['ID']]) ? "new-posts-section.jpg" : "blank-section.jpg") . "\" alt=\"X\"></td>\n";
		print "<td><a href=\"page.board.php?id=" . urlencode($subboard['ID']) . "\">" . htmlentities($subboard['Label']) . "</a></td>\n";
		print "<td rowspan=\"2\" valign=\"middle\" align=\"center\" width=\"100\">{$subboard['TopicCount']} topic" . ($subboard['TopicCount'] == 1 ? "" : "s") . "<br>{$subboard['PostCount']} post" . ($subboard['PostCount'] == 1 ? "" : "s") . "</td>\n";
		print "<td rowspan=\"2\" valign=\"middle\" align=\"center\" width=\"200\">" . board_lastPost($subboard['LastPost']) . "</td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td>" . htmlentities($subboard['Description']) . "</td>\n";
		print "</tr>\n";
	}
	
	print "</table>\n";
	page_section();
}

page_section();
print "<a href=\"page.new.php?type=topic&amp;id=" . urlencode($id) . "\">New topic</a><br>";
print htmlentities($info['Description']);
page_section();

page_section("Topics");
?>

<table width="100%" cellspacing="0" cellpadding="0">
	<tr>
		<td width="40">&nbsp;</td>
		<td><b>Topic</b></td>
		<td width="100"><b>Responses</b></td>
		<td width="100"><b>Views</b></td>
		<td width="100"><b>Started by</b></td>
		<td width="200"><b>Last post</b></td>
	</tr>
<?php

$topics = db_query("SELECT * FROM `" . config_get("db_prefix") . "topics` WHERE `Board`='" . db_escape($id) . "' ORDER BY `LastPost` DESC;");
if(db_num_rows($topics) == 0)
{
	print "<tr><td colspan=\"6\" align=\"center\">&nbsp;<br>There are no topics on this board.<br>&nbsp;</td></tr>\n";
} else {
	while($topic = db_fetch_assoc($topics))
	{
		// Get the first post's information
		$post = db_query("SELECT * FROM `" . config_get("db_prefix") . "posts` WHERE `TopicID`='" . db_escape($topic['ID']) . "' ORDER BY `Time` ASC;");
		$post = db_fetch_assoc($post);
		
		$unread = db_query("SELECT * FROM `" . config_get("db_prefix") . "unread` WHERE `UserID`='" . sess_get("UserID") . "' AND `TopicID`='" . db_escape($topic['ID']) . "';");
		if(db_num_rows($unread) == 0)
		{
			$unread = 0;
		} else {
			$unread = 1;
		}
		
		print "<tr>\n";
		print "<td valign=\"top\"><img src=\"img/" . ($unread ? "new-posts-section.jpg" : "blank-section.jpg") . "\" alt=\"X\"></td>\n";
		print "<td valign=\"top\"><a href=\"page.topic.php?id=" . urlencode($topic['ID']) . "\">" . htmlentities($post['Title']) . "</a></td>\n";
		print "<td valign=\"top\">{$topic['PostCount']}</td>\n";
		print "<td valign=\"top\">{$topic['ViewCount']}</td>\n";
		print "<td valign=\"top\"><a href=\"page.user.php?id=" . urlencode($topic['UserID']) . "\">" . htmlentities(user_name($topic['UserID'])) . "</a></td>\n";
		print "<td valign=\"top\">" . board_lastPost($topic['LastPost']) . "</td>\n";
		print "</tr>\n";
	}
}

?>
</table>

<?php
page_section();

page_footer('$Id: page.board.php 133 2007-09-07 00:32:52Z jonathon.keogh $');

?>